#------------------------------------------------------------------------------
# Import libraries
#------------------------------------------------------------------------------

rm(list=ls())
library(LalRUtils)
libreq(tidyverse, data.table, zoo, tictoc, fst, fixest, PanelMatch, patchwork,
       rio, magrittr, janitor, did, panelView, ggiplot, tictoc, binsreg, interflex)
set.seed(42)
theme_set(lal_plot_theme())

#------------------------------------------------------------------------------




#------------------------------------------------------------------------------
# Define Paths
#------------------------------------------------------------------------------

# R studio
setwd( dirname(rstudioapi::getActiveDocumentContext()$path) )
# R default : unccoment if you use default R
# setwd(getSrcDirectory(function(){})[1])
#------------------------------------------------------------------------------



#------------------------------------------------------------------------------
# Load Data
#------------------------------------------------------------------------------

vcf <- fread("vcf_data_complete.csv", sep = ",")
setnames( vcf, "d", "D")
vcf_data <- copy( vcf )
gfc <- fread("gfc_dta.csv" )

#------------------------------------------------------------------------------



#------------------------------------------------------------------------------
# Load data
#------------------------------------------------------------------------------


# %% GFC mining
gfc = gfc[pref == 1] # subset to primary sample

# %% data prep for interflex
gfc[, `:=`(id_f   = as.factor(village),
           sty_f  = as.factor(styear),
           D_f    = as.factor(D)
)]
gfc[, distance := min_dist_to_mine * 110]
gfc[, mines_count_fac := as.factor(case_when(
  mines_in_5k %in% c(1,2) ~ "1-2",
  mines_in_5k %in% c(3,4) ~ "3-4",
  mines_in_5k > 5 ~ "5+",
  TRUE ~ "0"))]

# %% intensive margin regression
m00 = feols(def_ha ~ D * mines_in_5k      | village[t] + styear, cluster = ~nameb, gfc)
m01 = feols(def_ha ~ D * mines_count_fac  | village[t] + styear, cluster = ~nameb, gfc)

gfc[, ever_treated := max(D), .(village)]
ctrl_mean_gfc = round(gfc[ever_treated == 0 & 
                                  pesa_exposure == 0, 
                                mean(def_ha)], 2)
treat_mean_gfc  = round(gfc[ ever_treated == 1 & 
                                     pesa_exposure == 0, 
                                   mean(def_ha)], 2)
mods = list(m00, m01)
# %%
"village" %in% names(gfc)
treatmap =c(
  # GFC
  "def_ha"                 = "Annual Deforestation in Hectares",
  "id_f"                   = "Village",
  "village"                = "Village",
  "village[t]"             = "Village + Village TT",
  # both
  "yr"                     = "Year",
  "year"                   = "Year",
  "styear"                 = "State $\\times$ Year",
  "D"                      = "PESA $\\times$ Scheduled",
  "mines_in_5k"            = "\\# Mines in within 5km of village",
  "mines_count_fac1-2"     = "Scheduled $\\times$ 1-2 Mines in village",
  "mines_count_fac3-4"     = "Scheduled $\\times$ 3-4 Mines in village",
  "mines_count_fac5+"      = "Scheduled $\\times$ 5+ Mines in village",
  "nameb"                  = "Block"
)
# %%

fitstat_register("n_new", function(x) summary( x )$nobs , 
                 "\\# Observations")

etable(mods,
       style.tex = style.tex(main = "aer", 
                             depvar.title = "", 
                             model.title = "", 
                             var.title = "\\midrule", 
                             slopes.title = "\\midrule \\emph{Time Trends}", 
                             yesNo = c("$\\checkmark$", "")),
       signif.code = NA,
       fixef_sizes = T, 
       fixef_sizes.simplify = F,
       fitstat = c( "n_new", "r2" ), 
       tex = TRUE,
       dict = treatmap,
       extralines=list(
         "\\midrule \\emph{Summary Statistics}" = c( "", ""  ),
         "Mean Pre-Y (Non-Sch)"= c( rep( ctrl_mean_gfc, 2 ) ),
         "Mean Pre-Y (Sch )"   = c( rep( treat_mean_gfc, 2 ) ),
         "Dataset"     = c( rep( "GFC", 2 ) ),
         "Timespan"    = c( rep( "2001-2017", 2 ) )
       ),
       label = "tab:regs_mining_intensive",
       title = glue::glue("Regression estimates decomposed number of mines 
                          within 5km radius (ex-ante median cutoff)"),
       file = "appendix_tableA7.tex", 
       replace = TRUE
)

#------------------------------------------------------------------------------